package cn.edu.cjw.APMFP.Util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {

	public final static String DATE_TIME = "yyyy-MM-dd HH:mm:ss";

	public final static String DATE_STR = "yyyy-MM-dd";

	public static final String DATETIME_MS = "yyyyMMddHHmmssSSS";

	public static final String DATE_SLASH_STR = "yyyy/MM/dd";

	public final static int SECOND = 1000;

	public final static int MINUTE = 60 * SECOND;

	public final static int HOUR = 60 * MINUTE;

	public final static int DAY = 24 * HOUR;


	/**
	 * 格式化字符串日期，转换成Date
	 * @param date  字符串日期
	 * @param pattern 默认 yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static Date parse(String date, String pattern){
		String p = pattern == null ? DATE_TIME : pattern;
		DateFormat dateFormat = new SimpleDateFormat(p);
		Date d = null;
		try {
			d = dateFormat.parse(date);
		} catch (ParseException e) {
			System.err.println("parse date string error, date:" + date + ", pattern:" + p + ", msg: " + e.getMessage());
		}
		return d;
	}

	/**
	 * 日期按照指定格式转换成字符串
	 * @param date  日期
	 * @param pattern 默认 yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static String format(Date date, String pattern){
		DateFormat dateFormat = new SimpleDateFormat(pattern == null ? DATE_TIME : pattern);
		return dateFormat.format(date);
	}

}
